﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.IO;
using System.Windows;
using Microsoft.Windows.Controls;
using WpfRichText.Ex.XamlToHtmlParser;

namespace inzDesktop
{
    public class XAMLToHtmlFormatter : ITextFormatter
    {

        public string GetText(FlowDocument document)
        {

            TextRange textRange = new TextRange(document.ContentStart, document.ContentEnd);
            MemoryStream output = new MemoryStream();

            textRange.Save(output, DataFormats.Xaml);
            string xaml = UTF8Encoding.UTF8.GetString(output.ToArray());


            string html = String.Empty;
            if (!string.IsNullOrEmpty(xaml))
            {
                html = HtmlFromXamlConverter.ConvertXamlToHtml(xaml);
            }
            return html;

        }

        public void SetText(FlowDocument document, string text)
        {                        
            string xaml = string.Empty;
            xaml = HtmlToXamlConverter.ConvertHtmlToXaml(text, false);           

            TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
            
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xaml)))
            {
                tr.Load(ms, DataFormats.Xaml);
            }
        }
    }
}

